package leetcode

func maxProfit(prices []int) int {
	count := len(prices)
	if count < 2 {
		return 0
	}

	min, profit, pos := prices[0], 0, 0
	for i := 1; i < count; i++ {
		pos = prices[i] - prices[i-1]
		if pos < 0 {
			profit += prices[i-1] - min
			min = prices[i]
		} else if pos > 0 && prices[i-1] < min {
			min = prices[i]
		}
	}

	if pos >= 0 && prices[count-1] > min {
		profit += prices[count-1] - min
	}

	return profit
}
